/*main.c*/
//older version of LCD test, deprecated

#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "../diag.h"
#include "../osram96x16.h"
#include "lcd.h"
#include "font.h"
#include "lcd12.h"

#define USER_LED GPIO_PIN_5
#define LCD_CS GPIO_PIN_0
#define LCD_RST GPIO_PIN_1
#define LCD_CLK GPIO_PIN_2
#define LCD_DATA GPIO_PIN_5
typedef unsigned char byte;

void dly1(void){
 long int i;
 for (i=0;i<400000;i++){ ; }
}
void dly2(int a){
 long int i;
 long int j;
 j=10000*a;
 for (i=0;i<j;i++){ ; }
}


word buffer[32][32];
void bresenham_circle(int xc, int yc, int r, byte c);

void clear(word *buffer){
 int i;
 for(i=0;i<50*50;i++) buffer[i]=0;
}



int main(void){
    //
    // Set the clocking to run directly from the crystal.
    //
	int dly=1;
	int i,j;

    SysCtlClockSet(SYSCTL_SYSDIV_2 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
				   SYSCTL_INT_OSC_DIS |	
                   SYSCTL_XTAL_6MHZ);

//    OSRAMInit(false);

    //
    // Hello!
    //

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

	GPIODirModeSet(GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_OUT);

	LCD_init(); 

//LCD_hline(0,131,10,0x0fff);

for(i=0;i<16;i++){
 for (j=0;j<16;j++){
   buffer[i][j]=i+16*j;
   buffer[i][j+16]=i+16*j+256*4;
   buffer[i+16][j]=i+16*j+256*8;
   buffer[i+16][j+16]=i+16*j+256*12;
 }
}



	
//for (i=33;i<66;i++) LCD12_hline(0,132,i,0x0fff); //white
//for (i=66;i<99;i++) LCD12_hline(0,132,i,0x0f00); //red
LCD12_rectangle(0,33,131,65,0x0fff);
LCD12_rectangle(0,66,131,98,0x0f00);

for (i=0;i<33;i++) LCD12_hline(0,i<<1,33+i,0x000f);
for (i=0;i<33;i++) LCD12_hline(0,(33-i)<<1,66+i,0x000f);

for (i=0;i<16;i++) {
	LCD12_hline(0,19,i+114,i);
	LCD12_hline(20,39,i+114,i*16);
	LCD12_hline(40,59,i+114,i*256);
	LCD12_hline(60,79,i+114,i*17);
	LCD12_hline(80,99,i+114,i*0x0101);
	LCD12_hline(100,119,i+114,i*0x0110);
}

LCD12_char(10,10,0x0f00,0x0008,'H');
LCD12_char(18,10,0x0f00,0x0008,'e');
LCD12_char(26,10,0x0f00,0x0008,'l');
LCD12_char(34,10,0x0f00,0x0008,'l');
LCD12_char(42,10,0x0f00,0x0008,'o');
LCD12_char(50,10,0x0f00,0x0008,'!');
LCD12_string(0,0,0x0fff,0x000f,"How are you?");

//LCD12_blit(98,98,32,32,(word *)buffer);

LCD_8bpp();
LCD_palette();
for (i=0;i<16;i++) {
	LCD_hline(0,19,i+99,color8(i));
	LCD_hline(20,39,i+99,color8(i*16));
	LCD_hline(40,59,i+99,color8(i*256));
	LCD_hline(60,79,i+99,color8(i*0x0011));
	LCD_hline(80,99,i+99,color8(i*0x0101));
	LCD_hline(100,119,i+99,color8(i*0x0110));
}
LCD_line(0,0,130,130,0xfc);
LCD_line(0,130,130,0,0x1f);
LCD_circle(66,66,50,0xe3);
LCD_12bpp();

//LCD_vline(66,0,130,0xe3);  
/*
  for (i=0;i<132;i++){
    LCD_put_pixel(0xe0,i,5);
    LCD_put_pixel(0x1c,i,10);
    LCD_put_pixel(0x03,i,15);
    LCD_put_pixel(0xfc,5,i);
  }
*/

dly=0;
while(1){
 GPIOPinWrite(GPIO_PORTC_BASE, USER_LED, USER_LED);
 LCD12_blit(98,0,32,32,(word *)buffer);
 dly2(dly);
 GPIOPinWrite(GPIO_PORTC_BASE, USER_LED, 0x00);
 dly2(dly);

 clear((word *)buffer);

 buffer[dly][10]=0x0ff0;
 buffer[dly][11]=0x0ff5;
 buffer[dly][12]=0x0ffa;
 buffer[dly][13]=0x0fff;
 dly++;
 if (dly==32) dly=0;
}
}



